/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */

package com.ecarx.systemui.plugin.utils;


import com.ecarx.systemui.plugin.controller.hvac.TempSignalController;

import java.util.HashMap;
import java.util.Map;

public class ClimateTempUtils {

    public static int getProgressByTemp(boolean isCELSIUS, float temp) {
        if (isCELSIUS) {
            if (temp < 15.5f) {
                return 0;
            }

            if (temp > 28.5f) {
                return 26;
            }

            return tempValueMap.get(temp);
        } else {
            temp = Math.round(temp);
            if (temp < 59f) {
                return 0;
            }

            if (temp > 85f) {
                return 26;
            }

            return tempValueMap_F.get(temp);
        }
    }


    public static Float getTempByProgress(boolean isCELSIUS, int progress) {
        if (isCELSIUS) {
            if (progress < 0) {
                return TempSignalController.MIN_TEMP_C;
            }

            if (progress > 26) {
                return TempSignalController.MAX_TEMP_C;
            }

            return progressValueMap.get(progress);
        } else {
            if (progress < 0) {
                return TempSignalController.MIN_TEMP_F;
            }

            if (progress > 26) {
                return TempSignalController.MAX_TEMP_F;
            }

            return progressValueMap_F.get(progress);
        }
    }

    public static int getMaxProgress() {
        return tempValueMap.size() - 1;
    }

    private static final HashMap<Float, Integer> tempValueMap = new HashMap<Float, Integer>() {
        {
            put(15.5f, 0);
            put(16.0f, 1);
            put(16.5f, 2);
            put(17.0f, 3);
            put(17.5f, 4);
            put(18.0f, 5);
            put(18.5f, 6);
            put(19.0f, 7);
            put(19.5f, 8);
            put(20.0f, 9);
            put(20.5f, 10);
            put(21.0f, 11);
            put(21.5f, 12);
            put(22.0f, 13);
            put(22.5f, 14);
            put(23.0f, 15);
            put(23.5f, 16);
            put(24.0f, 17);
            put(24.5f, 18);
            put(25.0f, 19);
            put(25.5f, 20);
            put(26.0f, 21);
            put(26.5f, 22);
            put(27.0f, 23);
            put(27.5f, 24);
            put(28.0f, 25);
            put(28.5f, 26);
        }
    };

    private static final HashMap<Float, Integer> tempValueMap_F = new HashMap<Float, Integer>() {
        {
            put(59f, 0);
            put(60f, 1);
            put(61f, 2);
            put(62f, 3);
            put(63f, 4);
            put(64f, 5);
            put(65f, 6);
            put(66f, 7);
            put(67f, 8);
            put(68f, 9);
            put(69f, 10);
            put(70f, 11);
            put(71f, 12);
            put(72f, 13);
            put(73f, 14);
            put(74f, 15);
            put(75f, 16);
            put(76f, 17);
            put(77f, 18);
            put(78f, 19);
            put(79f, 20);
            put(80f, 21);
            put(81f, 22);
            put(82f, 23);
            put(83f, 24);
            put(84f, 25);
            put(85f, 26);
        }
    };

    private static final HashMap<Integer, Float> progressValueMap = new HashMap<Integer, Float>() {
        {
            put(0, 15.5f);
            put(1, 16.0f);
            put(2, 16.5f);
            put(3, 17.0f);
            put(4, 17.5f);
            put(5, 18.0f);
            put(6, 18.5f);
            put(7, 19.0f);
            put(8, 19.5f);
            put(9, 20.0f);
            put(10, 20.5f);
            put(11, 21.0f);
            put(12, 21.5f);
            put(13, 22.0f);
            put(14, 22.5f);
            put(15, 23.0f);
            put(16, 23.5f);
            put(17, 24.0f);
            put(18, 24.5f);
            put(19, 25.0f);
            put(20, 25.5f);
            put(21, 26.0f);
            put(22, 26.5f);
            put(23, 27.0f);
            put(24, 27.5f);
            put(25, 28.0f);
            put(26, 28.5f);
        }
    };

    private static final HashMap<Integer, Float> progressValueMap_F = new HashMap<Integer, Float>() {
        {
            put(0, 59f);
            put(1, 60f);
            put(2, 61f);
            put(3, 62f);
            put(4, 63f);
            put(5, 64f);
            put(6, 65f);
            put(7, 66f);
            put(8, 67f);
            put(9, 68f);
            put(10, 69f);
            put(11, 70f);
            put(12, 71f);
            put(13, 72f);
            put(14, 73f);
            put(15, 74f);
            put(16, 75f);
            put(17, 76f);
            put(18, 77f);
            put(19, 78f);
            put(20, 79f);
            put(21, 80f);
            put(22, 81f);
            put(23, 82f);
            put(24, 83f);
            put(25, 84f);
            put(26, 85f);
        }
    };

    /**
     * 空调温度摄氏度和华氏度转换映射表
     */
    private static final Map<String, String> C_F_TABLE = new HashMap<String, String>() {
        {
            put("Lo", "Lo");// temp < 16/60
            put("16.0", "60");
            put("16.5", "61");
            put("17.0", "62");
            put("17.5", "63");
            put("18.0", "64");
            put("18.5", "65");
            put("19.0", "66");
            put("19.5", "67");
            put("20.0", "68");
            put("20.5", "69");
            put("21.0", "70");
            put("21.5", "71");
            put("22.0", "72");// default
            put("22.5", "73");
            put("23.0", "74");
            put("23.5", "75");
            put("24.0", "76");
            put("24.5", "77");
            put("25.0", "78");
            put("25.5", "79");
            put("26.0", "80");
            put("26.5", "81");
            put("27.0", "82");
            put("27.5", "83");
            put("28.0", "84");
            put("Hi", "Hi"); // temp >28/84
        }
    };

}
